package com.example.demo.controller;

import com.example.demo.common.JsonResult;
import com.example.demo.common.UserSessionUtils;
import com.example.demo.entity.Assess;
import com.example.demo.service.AssessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/assess")
public class AssessController {

    @Autowired
    private AssessService assessService;

    @RequestMapping("/getlist")
    public JsonResult getList(Integer dishId) {
        if (dishId == null || dishId <= 0) {
            return JsonResult.failure(-1, "非法参数");
        }
        return JsonResult.success(assessService.getList(dishId));
    }

    @RequestMapping("/add")
    public JsonResult add(HttpServletRequest request, Assess assess) {
        if (assess == null || assess.getOrderId() == null || assess.getOrderId() <= 0 || assess.getDishId() == null
                || assess.getDishId() <= 0 || assess.getGrade() == null || assess.getGrade() <= 0 || assess.getGrade() > 5) {
            return JsonResult.failure(-1, "非法参数");
        }
        return JsonResult.success(assessService.add(assess, UserSessionUtils.getUserId(request)));
    }

    @RequestMapping("/admin/delete")
    public JsonResult delete(Integer orderId, Integer dishId) {
        if (orderId == null || dishId == null) {
            return JsonResult.failure(-1, "非法参数");
        }
        return JsonResult.success(assessService.delete(orderId, dishId));
    }
}
